package com.base.testmvvm;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import java.util.Random;

public class UserViewModel extends ViewModel implements BaseViewModel<User> {
    private MutableLiveData<User> liveUser;
    @Override
    public User loadData() {
        Random r = new Random();
        User user = new User();
        user.setId(r.nextInt());
        user.setName("jack");
        user.setPhone("189101928374");
        return user;
    }

    @Override
    public void clearData() {

    }

    @Override
    public void changeData() {
        if (liveUser != null) {
            liveUser.setValue(loadData());
        }
    }

    @Override
    public MutableLiveData<User> getData() {
        if (liveUser == null) {
            liveUser = new MutableLiveData<>();
        }

        liveUser.setValue(loadData());
        return liveUser;
    }

    @Override
    public void setLiveData(MutableLiveData<User> liveData) {
        liveUser = liveData;
    }


}
